
  <template>
    <el-dialog title="编辑退换货记录" :visible.sync="dialogVisible" width="1200px" :close-on-click-modal="false"
      :modal-append-to-body="false" v-drag2anywhere>
      <div>
        <div >
          <div class="title">采购明细</div>
          <el-table :data="tableData" ref="filterTable" border empty-text="结果为空" height="100%">
            <el-table-column label="申请时间" prop="req_date" width="140px"></el-table-column>
            <el-table-column label="付款单号" prop="buycode" width="100px"></el-table-column>
            <el-table-column label="品牌" prop="brand"  width="60" show-overflow-tooltip></el-table-column>
            <el-table-column label="商品名称" prop="title" show-overflow-tooltip></el-table-column>
            <el-table-column label="规格型号" prop="model" show-overflow-tooltip></el-table-column>
            <el-table-column label="单位" prop="unit" width="50px" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="数量" prop="count" width="50px" align="center" ></el-table-column>
            <el-table-column label="单价" prop="price" width="85px" align="center"></el-table-column>
            <el-table-column label="总价" prop="total" width="85px" align="center"></el-table-column>
            <el-table-column label="发票" align="center"  width="120px" show-overflow-tooltip>
              <template v-slot="scope">
                {{scope.row.tickettype}}({{scope.row.rate}})
              </template>
            </el-table-column>
            <el-table-column label="运费" prop="yunfei" width="120px" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="供应商名称" prop="seller" show-overflow-tooltip></el-table-column>
          </el-table>
        </div>
      </div>

      <div style="margin-top: 20px;">
        <div class="title">退换货信息</div>
        <el-form :model="formData" ref="form1" label-width="100px" :rules="rules" >
          <el-row :gutter="15">
            <el-col :span="6">
              <el-form-item label="采购合同号:">
                {{formData.buycode}}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="退款类型:" prop="type">
                <el-select v-model="formData.type" placeholder="请选择类型">
                  <el-option label="仅退款" value="1"></el-option>
                  <el-option label="仅退货" value="2"></el-option>
                  <el-option label="退款退货" value="3"></el-option>
                  <el-option label="换货" value="4"></el-option>
                  <el-option label="优惠返点" value="5"></el-option>
                  </el-select>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="退款金额:"  prop="amount">
                <el-input-number v-model="formData.amount" :precision="2" :min="0" :max="formData.total" style="width:100%"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="退款状态:" prop="status">
                <el-select v-model="formData.status" placeholder="请选择状态">
                  <el-option label="退款中" value="0"></el-option>
                  <el-option label="已完成" value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="18">
              <el-form-item label="备注信息:" prop="memo">
               <el-input type="textarea" v-model="formData.memo" placeholder="退款备注:填写退款原因,退款情况等信息" :rows="3"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="退款日期:" prop="done_date">
                <el-date-picker v-model="formData.done_date" value-format="yyyy-MM-dd" type="date" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer">
        <el-button size="medium">取消</el-button>
        <el-button type="primary" size="medium" @click="handleSave" >保存</el-button>
      </div>
    </el-dialog>
  </template>

  <script>
    export default {
      data() {
        return {
          index: -1,
          formData:{
            buycode:'',
            type:'',
            amount:0,
            status:'0',
            done_date:'',
            tickettype:'',
            ticketrate:'',
            memo:''
          },
          tableData: [],
          dialogVisible: false,
          rules:{
            type:[{required:true,message:"请选择退款类型",trigger:'change'}],
            status:[{required:true,message:"请选择退款状态",trigger:'change'}],
          }
        }
      },
      methods: {
        handleSizeChange: function(val) {
          this.pagesize = val;
          this.curpage = 1;
          this.loadDataList();
        },
        handleCurrentChange: function(val) {
          this.curpage = parseInt(val);
          this.loadDataList();
        },
        init(index,data) {
          this.index=index;
          data.amount=parseFloat(data.amount);
          data.total=parseFloat(data.total);
          this.formData=data;
          this.tableData=[data];
        },
        handleSave(){
            if(this.formData.status==1 && this.formData.done_date==""){
                this.$message.error({message:"完成状态必须填写完成的日期",duration:1500,showClose:true});
                return;
            }
            this.$refs.form1.validate(ok=>{
               if(ok){
                  const params={
                    id:this.formData.id,
                    buycode:this.formData.buycode,
                    buyid:this.formData.buyid,
                    buydid:parseInt(this.formData.buydid),
                    type:this.formData.type,
                    total:this.formData.total,
                    amount:parseFloat(this.formData.amount),
                    status:this.formData.status,
                    memo:this.formData.memo,
                    done_date:this.formData.done_date,
                    count:this.formData.count,
                    buyer:this.formData.buyer,
                    tickettype:this.formData.tickettype,
                    ticketrate:this.formData.ticketrate,
                    supname:this.formData.supname,
                    yunfei:this.formData.yunfei,
                    price:this.formData.price,
                    total:this.formData.total,
                    title:this.formData.title,
                    model:this.formData.model,
                    unit:this.formData.unit,
                    brand:this.formData.brand
                  }
                  this.axios.post("api/admin/buyexchange/update",params).then(res=>{
                    if(res.status==200 && res.data.code==1){
                       this.$message.success({message:res.data.msg,duration:1500,showClose:true});
                       this.dialogVisible=false;
                       this.$emit("update",this.index,params);
                    }else{
                      this.$message.error({message:res.data.msg,duration:1500,showClose:true});
                    }
                  })
               }
            })
        }
      }
    }
  </script>
